package com.pawernode.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;


@Service
@Slf4j
public class MessageService {


    @Resource
    private RabbitTemplate rabbitTemplate;


    @Value("${my.exchangeName}")
    private String exchangeName;

    public void sendMsg() {
//        for (int i = 1; i <= 1000; i++) {
//            String strMsg = "hello world" + i;
//            // 创建一个消息对象，内容为"hello rabbitmq"
//            Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
//                    .build();
//            // 将消息发送到指定的交换机和路由键
//            rabbitTemplate.convertAndSend(exchangeName, "info", message);
//        }

        for (int i = 1; i <= 5; i++) {
            String strMsg = "hello world" + i;
            // 创建一个消息对象，内容为"hello rabbitmq"
            Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
                    .build();
            // 将消息发送到指定的交换机和路由键
            rabbitTemplate.convertAndSend(exchangeName, "info", message);
        }
        {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setPriority(6); // 设置消息的优先级
            String strMsg = "hello world" + 6;
            // 创建一个消息对象，内容为"hello rabbitmq"
            Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
                    .andProperties(messageProperties)
                    .build();
            // 将消息发送到指定的交换机和路由键
            rabbitTemplate.convertAndSend(exchangeName, "info", message);
        }

        {
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setPriority(8); // 设置消息的优先级
            String strMsg = "hello world" + 7;
            // 创建一个消息对象，内容为"hello rabbitmq"
            Message message = MessageBuilder.withBody("hello rabbitmq".getBytes())
                    .andProperties(messageProperties)
                    .build();
            // 将消息发送到指定的交换机和路由键
            rabbitTemplate.convertAndSend(exchangeName, "info", message);
        }

        // 记录日志信息，消息发送完毕的时间
        log.info("消息发送完毕，发送时间为：{}", new Date());
    }
}
